package com.hope.bubble.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hope.bubble.mapper.BubbleTopicTagMapper;
import com.hope.bubble.model.entity.BubbleTag;
import com.hope.bubble.model.entity.BubbleTopicTag;
import com.hope.bubble.service.IBubbleTopicTagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文件名：BubbleTopicTagServiceImpl
 * 创建者：hope
 * 邮箱：1602774287@qq.com
 * 微信：hope4cc
 * 创建时间：2022/10/22-10:12
 * 描述：
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BubbleTopicTagServiceImpl extends ServiceImpl<BubbleTopicTagMapper, BubbleTopicTag> implements IBubbleTopicTagService {

    /**
     * 根据帖子的id  查询标签的集合
     * @param topicId TopicId
     * @return
     */
    @Override
    public List<BubbleTopicTag> selectByTopicId(String topicId) {
        QueryWrapper<BubbleTopicTag> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BubbleTopicTag::getTopicId, topicId);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     *
     * @param id  帖子id
     * @param tags 标签列表
     */
    @Override
    public void createTopicTag(String id, List<BubbleTag> tags) {
        // 先删除topicId对应的所有记录
        this.baseMapper.delete(new LambdaQueryWrapper<BubbleTopicTag>().eq(BubbleTopicTag::getTopicId, id));

        // 循环保存对应关联
        tags.forEach(tag -> {
            BubbleTopicTag topicTag = new BubbleTopicTag();
            topicTag.setTopicId(id);
            topicTag.setTagId(tag.getId());
            this.baseMapper.insert(topicTag);
        });
    }

}